package cn.z2huo.core.exception;

import cn.z2huo.core.status.StatusCode;
import cn.z2huo.core.status.second.SecondStatusCode;

import java.util.Optional;

/**
 * <p>基础异常
 */
public class BaseException extends RuntimeException {

    protected final StatusCode statusCode;

    protected SecondStatusCode secondStatusCode;

    public BaseException(StatusCode statusCode, String message) {
        super(message);
        this.statusCode = statusCode;
    }

    public BaseException(StatusCode statusCode, String message, Throwable cause) {
        super(message, cause);
        this.statusCode = statusCode;
    }

    public BaseException(StatusCode statusCode, SecondStatusCode secondStatusCode, String message) {
        super(message);
        this.statusCode = statusCode;
        this.secondStatusCode = secondStatusCode;
    }

    public BaseException(StatusCode statusCode, SecondStatusCode secondStatusCode, String message, Throwable cause) {
        super(message, cause);
        this.statusCode = statusCode;
        this.secondStatusCode = secondStatusCode;
    }

    @Override
    public String getLocalizedMessage() {
        return getMessage();
    }

    @Override
    public String getMessage() {
        return String.format("[%s][%s]: %s",
                Optional.ofNullable(statusCode).map(StatusCode::getCode).orElse(""),
                Optional.ofNullable(secondStatusCode).map(SecondStatusCode::getCode).orElse(""),
                super.getMessage());
    }

}
